package goldbachnumber;

import songuyenlon.SoNguyenLon;

public class GoldBachNumber {
	final static SoNguyenLon Mot = new SoNguyenLon("1");
	final static SoNguyenLon Hai = new SoNguyenLon("2");
	final static SoNguyenLon Khong = new SoNguyenLon("0");
    
	/*
     * Ham uoc luong gia tri can bac 2:
     * Input: so nguyen lon can uoc luong gia tri can bac 2.
     * Output: so nguyen lon la gia tri can uoc luong.
     */
	
    private static SoNguyenLon Can(SoNguyenLon A) {
	       int m = A.Dodai();
	        if(m%2 == 0){
	            m = (m-2)/2;
	            int n = m/9 +1;
	            int[] ma = new int[n];
	            ma[0] = (int) (6*Math.pow(10, m%9));
	            return new SoNguyenLon(ma,n);
		}
		else{
	            m = m/2;
	            int n = m/9 +1;
	            int[] ma = new int[n];
	            ma[0] = (int) (2*Math.pow(10, m%9));
	            return new SoNguyenLon(ma,n);
		}
	}
    
    /*
     * Ham kiem tra so nguyen co phai la so nguyen to hay khong:
     * Input: so nguyen lon can kiem tra.
     * Output: neu la so nguyen tra ve true, nguoc lai tra ve false.
     */
	
	private static boolean CheckPrimeNumber(SoNguyenLon A){
	     SoNguyenLon B = Hai;
	     boolean kiemtra = true;
	     SoNguyenLon A1 = new SoNguyenLon();
	     if(A.Sosanh(new SoNguyenLon("10")) == -1)
	         A1 = A.Tru(Mot);
	     else 
	         A1 = Can(A);
	     while(B.Sosanh(A1) <= 0){
	         if(A.Phandu(B).Sosanh(Khong) == 0){
	             return kiemtra = false;
	         }
	         B = B.Cong(Mot);
	     }
	     return kiemtra;
	}
	
	/*
	 * Ham main()
	 * Kiem tra gia thuyet GoldBach voi so nguyen lon A
	 * Neu dung in ra A = A1 + A2 (trong do A1,A2 la 2 so nguyen to)
	 * Neu khong in ra "Khong ton tai A = A1 + A2"
	 */
	
    public static void main(String[] args){
        long start, stop;
        start = System.currentTimeMillis();
        SoNguyenLon A = new SoNguyenLon("10000000000000000000"); //Nhap so can kiem tra
        SoNguyenLon A1 = new SoNguyenLon("3");
        boolean key = false;
        if(A.Sosanh(new SoNguyenLon("4")) == 0){
            System.out.println("A = A1 + A2");
            System.out.print("A = ");A.In();
            System.out.print("A1 = ");Hai.In();
            System.out.print("A2 = ");Hai.In();
            key = true;
        }
        else{
            while(A1.Sosanh(A.Chia(Hai)) <= 0){
            	if(CheckPrimeNumber(A1)){
                    SoNguyenLon A2 = A.Tru(A1);
                    if(CheckPrimeNumber(A2)){
                    	System.out.println("A = A1 + A2");
                    	System.out.print("A = ");A.In();
                    	System.out.print("A1 = ");A1.In();
                    	System.out.print("A2 = ");A2.In();
                    	key = true;
                    	break;
                    }
            	}
            	A1 = A1.Cong(Hai);
            }
        }
        if(key = false)
            System.out.println("Khong ton tai A = A1 + A2");
        stop = System.currentTimeMillis();
        System.out.println("Thoi gian chay: "+(stop - start));
    }
}
